from Lib.SerialPort import *
from Lib.crc16 import *
from Lib.Utils import *
import datetime
import time
import os
import threading

class Beam(object):
    def __init__(self):
        self.ls1 = 0
        self.ls2 = 0
        self.ls3 = 0
        self.ls4 = 0
        self.beamValue=0
        self.theadBeam = None
        self.theadBeamFlag = True
        self.seria = SerialPort('/dev/serial_1',9600)

    def start(self):
        self.theadBeamFlag = True
        self.theadBeam=threading.Thread(target=self.beamWork)
        self.theadBeam.start()


    def readBeam(self):
        print('readBeam')
        self.seria.Write("getdatas\r\n".encode())
        encodestr = bytes.decode(self.seria.Read())
        print('beamval')
        print('beamval',encodestr)
        if(encodestr!='' and encodestr  is not None):
            try:
                self.ls1 = round(float(encodestr.split(',')[0]),1)
            except :
                self.ls1 = 0

            try:
                self.ls2 = round(float(encodestr.split(',')[1]),1)
            except :
                self.ls2 = 0

            try:
                self.ls3 = round(float(encodestr.split(',')[2]),1)
            except :
                self.ls3 = 0

            try:
                self.ls4 = round(float(encodestr.split(',')[3]),1)
            except :
                self.ls4 = 0

        else:
            self.seria = SerialPort('/dev/serial_1',9600)
            time.sleep(0.2)

        return [self.ls1,self.ls2,self.ls3,self.ls4]

    def beamWork(self):
        sameCount=0
        sameValue=0
        while self.theadBeamFlag:
            try:
                time.sleep(1)
                cTem = self.readBeam()
                if(cTem[0]==sameValue):
                    sameCount+=1
                sameValue=cTem[0]
                if(sameCount>=20000):
                    self.stop()
                    time.sleep(0.2)
                    self.setBeam(self.beamValue)
                    time.sleep(0.2)
                    self.setBeam(self.beamValue)
                    time.sleep(0.2)
                    self.start()
                    sameCount=0
            except:
                try:
                    self.seria = SerialPort('/dev/serial_1',9600)
                except :
                    pass



    def setBeam(self,beamValue):
        self.beamValue=beamValue
        try:
            print("setBeat set start-",beamValue)
            self.seria.Write(("set_"+beamValue+'\r\n').encode())
            readStr=bytes.decode(self.seria.Read())
            print(readStr)
        except :
            self.seria = SerialPort('/dev/serial_1',9600)
            time.sleep(0.2)
            self.seria.Write(("set_"+beamValue+'\r\n').encode())
            self.seria.Read()


    def stop(self):
        self.theadBeamFlag = False
        time.sleep(2)
        try:
            self.seria.Write(("set_0000,0000,0000,0000\r\n").encode())
            readStr=bytes.decode(self.seria.Read())
            print(readStr)
            #if(readStr[0:23]=="set_0000,0000,0000,0000"):
            #    print('stop')
            #    return
        except :
            try:
                self.seria = SerialPort('/dev/serial_1',9600)
            except :
                pass

        # self.seria.Read()


